<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="oitem">
    <div class="weui-cells__title">货品明细{{itemNo + 1}}
      <p id="acts" style="float: right;color: #1aad19;" v-if="isLast">
        <span id="addItem" @click="addItem" style="padding-right: 5px;">添加</span>
        <span v-if="itemNo != 0">|</span>
        <span id="delItem" style="padding-left: 5px;" @click="delItem" v-if="itemNo != 0">删除</span>
      </p>
    </div>
    <div class="weui-cell weui-cell_select fir">
      <div class="weui-cell__hd"><label class="weui-label">类型</label></div>
      <div class="weui-cell__bd">
        <select class="weui-select product" v-model="atype" :name="'items[' + itemNo + '].type'">
          <option value="2">成品入库</option>
          <option value="3">不合格品入库</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select">
      <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
      <div class="weui-cell__bd">
        <select class="weui-select product" v-model="currentProduct" :name="'items[' + itemNo + '].productid'">
          <option value="" disabled>请选择</option>
          <option selected=""  v-for="(item, index) in products" :key="item.guid" :value="item.productId">{{item.productName}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select">
      <div class="weui-cell__hd"><label class="weui-label">规格 </label></div>
      <div class="weui-cell__bd">
        <input type="hidden"  :name="'items[' + itemNo + '].specguid'" :value="spec.guid" />
        <input type="hidden"  :name="'items[' + itemNo + '].stockunit'" :value="spec.stockunit" />
        <input type="hidden"  :name="'items[' + itemNo + '].stockunitid'" :value="spec.stockunitid" />
        <select class="weui-select spec" v-model="spec">
          <option selected=""  v-for="(item, index) in specifications" :key="item.guid" :value="item">{{item.name}}</option>
        </select>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ApplySrItem',
  data () {
    return {
      value: '',
      defaultResult: [],
      products: [],
      currentProduct: '',
      specifications: [],
      atype: 2,
      spec: ''
    }
  },
  props: [
    'itemNo',
    'isLast',
    'type',
    'applyid'
  ],
  computed: {
  },
  watch: {
    'currentProduct': function(){
      this.getSpecByPro();
    },
    atype: function (val) {
      this.getProducts();
      this.getSpecByPro()
    },
    applyid: function (val) {
      this.getProducts();
    }
  },
  mounted() {
    this.getProducts();
  },
  methods: {
    addItem: function() {
      this.$emit('doAddItem');
    },
    delItem: function() {
      this.$emit('doDelItem');
    },
    getProducts: function(guid) {
      if (!this.applyid) return;
      let type = this.atype == 2 ? 5 : this.atype;
      this.axios({
        method: 'get',
        url: '/produce/Whinapply!getApplyInfo.action',
        params: {pickinglistid: this.applyid, productflag: type}
      }).then((response) => {
        if (response.data.msg == 'success') {
          let data = response.data;
          this.products = data.products;
        } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastText("获取产品数据失败");
        }

      })
    },
    product(index) {
      return 'product' + index;
    },
    onSearch (val) {
      this.$emit('doSearch',val)
    },
    onCancel () {
      this.$emit('doSearch', '')
    },
    onSelect (val) {
      this.value = val;
    },
    getSpecByPro: function() {
      this.axios({
        method: 'get',
        url: '/produce/Whinapply!getSpeByproduct.action',
        params: {productid: this.currentProduct, type: this.atype, stats: '1,4'}
      }).then((response) => {
        if (response.data.msg == 'success') {
          let data = response.data;
          this.specifications = data.specifications;
        } else {
          this.toastError("系统错误");
        }

      })
    },
  }
}
</script>
